﻿@page "/ChangePage"
@using System.ComponentModel
@using AntDesign.TableModels

@inject ChangeAreaServer changeAreaServer
@inject ChangeServer changeServer
@inject MessageService _message
@inject MaterialTypeServer materialTypeServer
<div>
    <Button @onclick="@Resh" Type="@ButtonType.Primary" Icon="@IconType.Outline.Reload">刷新</Button>
    <Button Danger OnClick="@(() => { Change = new ChangeEntity(); _visibleadd = true; })">添加</Button>
    <Button Danger OnClick="@(() => {
                                  if (selectedRows != null && selectedRows.Any())
                                  {
                                      Change = selectedRows.First().Clone();
                                      _visibleadd = true;
                                  }})">
        复制
    </Button>
    @if (selectedRows != null && selectedRows.Any())
    {
        <Button Danger OnClick="@(() => { _visible = true; })">删除</Button>
    }
    <Select DataSource="@ChangeAreas"
            AllowClear="true"
            ValueOnClear="@("")"
            @bind-Value="@select"
            ValueName="@nameof(ChangeAreasEntity.Code)"
            LabelName="@nameof(ChangeAreasEntity.Remark)">
    </Select>
</div>
<div>
    <Table @ref="table"
           TItem="ChangeEntity"
           DataSource="@Changes"
           Total="_total"
           @bind-PageIndex="_pageIndex"
           @bind-PageSize="_pageSize"
           @bind-SelectedRows="selectedRows">
        <Selection Key="@(context.id.ToString())" />
        <Column @bind-Field="@context.id" Sortable Hidden="true" />
        <Column Title="缓存区" Field="@context.ChangeAreaCode">
            <Select DataSource="@ChangeAreas"
                    AllowClear="true"
                    ValueOnClear="@("")"
                    @bind-Value="@context.ChangeAreaCode"
                    ValueName="@nameof(ChangeAreasEntity.Code)"
                    LabelName="@nameof(ChangeAreasEntity.Remark)">
            </Select>
        </Column>

        <Column Title="缓存架编码" Field="@context.DevId">
            <Input @bind-Value=@context.DevId />
        </Column>
        <Column Title="处于行数" Field=@context.row>
            <Select DataSource="@GlobalValues.Rows"
                    AllowClear="true"
                    ValueOnClear="@(0)"
                    @bind-Value="@context.row"
                    ValueName="@nameof(Val_Des.Val)"
                    LabelName="@nameof(Val_Des.Description)">
            </Select>
        </Column>

        <Column Title="干涉工位" Field=@context.Interference>
            <Input @bind-Value=@context.Interference />
        </Column>
        @*<Column Title="物料条码" Field="@context.BarCode">
            <Input @bind-Value=@context.BarCode />
        </Column>*@

        <Column Title="物料名称" Field=@context.MaterialName>
            <Input @bind-Value=@context.MaterialName />
        </Column>

        @*<Column Title="物料批次" Field=@context.Batch>
            <Input @bind-Value=@context.Batch />
        </Column>*@
        <Column Title="数量" Field=@context.quantity>
            <Input @bind-Value=@context.quantity />
        </Column>
        <Column Title="工位物料状态" Field=@context.MaterialType>
            <Select DataSource="@GlobalValues.MaterialStates"
                    AllowClear="true"
                    ValueOnClear="@(0)"
                    @bind-Value="@context.MaterialType"
                    ValueName="@nameof(Val_Des.Val)"
                    LabelName="@nameof(Val_Des.Description)">
            </Select>
        </Column>
        <Column Title="物料状态" Field=@context.State>
            <Select DataSource="@GlobalValues.States"
                    AllowClear="true"
                    ValueOnClear="@(0)"
                    @bind-Value="@context.State"
                    ValueName="@nameof(Val_Des.Val)"
                    LabelName="@nameof(Val_Des.Description)">
            </Select>
        </Column>

        @*<Column Title="物料拥者" Field=@context.owner>
            <Input @bind-Value=@context.owner />
        </Column>*@
        <Column Title="更新时间" Field=@context.updateTime>
            <Input @bind-Value=@context.updateTime />
        </Column>

        <ActionColumn>
            <Button @onclick="()=>OnChange(context)">更新</Button>
        </ActionColumn>
    </Table>

    <br />
    <p>PageIndex: @_pageIndex | PageSize: @_pageSize | Total: @_total</p>

    <br />
    <h5>selections:</h5>
    <Button OnClick="()=> { _pageIndex--; }">Previous page</Button>
    <Button OnClick="()=> { _pageIndex++; }">Next Page</Button>
    <Modal Title="@title"
           Visible="@_visible"
           OnOk="@HandleOk"
           OnCancel="@HandleCancel">
        <p>确认删除？</p>
    </Modal>
    <Modal Title="@titleadd"
           Visible="@_visibleadd"
           OnOk="@HandleOkadd"
           OnCancel="@HandleCanceladd">
        <Form Model="Change">
            <FormItem Label="缓存区">
                <Select DataSource="@ChangeAreas"
                        AllowClear="true"
                        ValueOnClear="@("")"
                        @bind-Value="@context.ChangeAreaCode"
                        ValueName="@nameof(ChangeAreasEntity.Code)"
                        LabelName="@nameof(ChangeAreasEntity.Remark)">
                </Select>
            </FormItem>
            <FormItem Label="缓存架编码">
                <Input @bind-Value=@context.DevId />
            </FormItem>

            <FormItem Label="处于行数" >
                <Select DataSource="@GlobalValues.Rows"
                        AllowClear="true"
                        ValueOnClear="@(0)"
                        @bind-Value="@context.row"
                        ValueName="@nameof(Val_Des.Val)"
                        LabelName="@nameof(Val_Des.Description)">
                </Select>
            </FormItem>

            <FormItem Label="干涉工位" >
                <Select DataSource="@Changes"
                        AllowClear="true"
                        ValueOnClear="@("")"
                        @bind-Value="@context.Interference"
                        ValueName="@nameof(ChangeEntity.DevId)"
                        LabelName="@nameof(ChangeEntity.DevId)">
                </Select>
            </FormItem>
            @*<FormItem Label="物料条码">
            <Input @bind-Value=@context.BarCode />
        </FormItem>*@

            <FormItem Label="物料名称">
                <Input @bind-Value=@context.MaterialName />
            </FormItem>

            <FormItem Label="数量">
                <Input @bind-Value=@context.quantity />
            </FormItem>
            @*<FormItem Label="物料批次">
            <Input @bind-Value=@context.Batch />
        </FormItem>*@
            <FormItem Label="工位物料状态">
                <Select DataSource="@GlobalValues.MaterialStates"
                        AllowClear="true"
                        ValueOnClear="@(0)"
                        @bind-Value="@context.State"
                        ValueName="@nameof(Val_Des.Val)"
                        LabelName="@nameof(Val_Des.Description)">
                </Select>
            </FormItem>
            <FormItem Label="物料状态">
                <Select DataSource="@GlobalValues.States"
                        AllowClear="true"
                        ValueOnClear="@(0)"
                        @bind-Value="@context.State"
                        ValueName="@nameof(Val_Des.Val)"
                        LabelName="@nameof(Val_Des.Description)">
                </Select>
            </FormItem>

            @*<FormItem Label="物料拥者">
            <Input @bind-Value=@context.owner />
        </FormItem>*@
        </Form>
    </Modal>
</div>

@using System.Text.Json;
@code {
    private string select = "";
    private bool ReshFlag = false;
    ObservableCollection<ChangeEntity> Changes { get; set; } = new ObservableCollection<ChangeEntity>();
    ObservableCollection<ChangeAreasEntity> ChangeAreas { get; set; } = new ObservableCollection<ChangeAreasEntity>();
    ObservableCollection<ChangeEntity> Changestemp { get; set; } = new ObservableCollection<ChangeEntity>();

    IEnumerable<ChangeEntity> selectedRows;
    ITable table;

    int _pageIndex = 1;
    int _pageSize = 10;
    int _total = 0;
    string title = "危险提示！！！";
    bool _visible = false;

    private async void HandleOk(MouseEventArgs e)
    {
        @foreach (var selected in selectedRows)
        {
            bool flag = await changeServer.Delete(selected.id);
            if (flag)
            {
                _message.Info($"CODE{selected.DevId}-删除成功");
            }
            else
            {
                _message.Info($"CODE{selected.DevId}-删除失败");
            }
        }
        selectedRows = null;
        _visible = false;
        await init();
    }

    private void HandleCancel(MouseEventArgs e)
    {
        selectedRows = null;
        _visible = false;
    }

    private ChangeEntity Change = new ChangeEntity();
    string titleadd = "添加缓存架";
    bool _visibleadd = false;

    private async void HandleOkadd(MouseEventArgs e)
    {
        bool flag = await changeServer.ADD(Change);
        if (flag)
        {
            _message.Info("添加成功");
        }
        else
        {
            _message.Info("添加失败");
        }
        _visibleadd = false;
        await init();
    }

    private void HandleCanceladd(MouseEventArgs e)
    {

        _visibleadd = false;
    }

    protected override async Task OnInitializedAsync()
    {
        var list = await changeAreaServer.GetChangeAreas();
        if (list != null )
        {
            foreach (var item in list)
            {
                ChangeAreas.Add(item);
            }

            var List = await changeServer.GetChanges();
            foreach (var item in List)
            {
                Changestemp.Add(item);
            }
            if (List != null)
            {
                foreach (var item in List)
                {
                    Changes.Add(item);
                }
            }
        }
        base.OnInitialized();
    }


    public async void OnChange(ChangeEntity Change)
    {
        ResultAPi result = await changeServer.UpData(Change);
        if (result.State)
        {
            _message.Info($"CODE{Change.DevId}-更新成功-----{result.Msg}");
        }
        else
        {
            _message.Info($"CODE{Change.DevId}-更新失败-----{result.Msg}");
        }
        init();
    }

    public void RemoveSelection(int id)
    {
        // var selected = selectedRows.Where(x => x.Id != id);
        // selectedRows = selected;
    }

    private void Delete(int id)
    {
        //forecasts = forecasts.Where(x => x.Id != id).ToArray();
        // _total = forecasts.Length;
    }
    private async void Resh()
    {
        if (!ReshFlag)
        {
            ReshFlag = true;

            await init();
            ReshFlag = false;
        }

    }

    private async Task Clear()
    {
        if (!ReshFlag)
        {
            ReshFlag = true;
            await init();
            ReshFlag = false;
        }
    }

    async Task init()
    {
        ChangeAreas.Clear();
        Changes.Clear();

        var list = await changeAreaServer.GetChangeAreas();
        if (list != null)
        {
            foreach (var item in list)
            {
                ChangeAreas.Add(item);
            }

            var List = await changeServer.GetChanges();
            foreach (var item in List)
            {
                Changestemp.Add(item);
            }
            if (select != "")
            {
                List = List.Where(x => x.ChangeAreaCode == select);
            }
            if (List != null)
            {
                foreach (var item in List)
                {
                    Changes.Add(item);
                }
            }
        }
        await this.InvokeAsync(() =>
        {
            this.StateHasChanged();
        });
    }

}
